﻿Imports System.Data.OleDb
Imports System.Xml
Imports System.ServiceModel.Syndication
Partial Public Class rss
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.ContentType = "application/rss+xml"

        Dim myFeed As New SyndicationFeed

        myFeed.Title = TextSyndicationContent.CreatePlaintextContent("Movies Blogspot Feed")
        myFeed.Description = TextSyndicationContent.CreatePlaintextContent _
        ("The five most recent articles added to the Movies Blogspot news website")
        myFeed.Links.Add(SyndicationLink.CreateSelfLink _
         (New Uri(okURL("List.aspx"))))
        myFeed.Language = "en-us"


        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("MovieConnectionString").ToString())
        conn.Open()
        Dim cmdquery As String = "select top 5 * from moviedetails"
        Dim dbcmd = New OleDb.OleDbCommand(cmdquery, conn)
        Dim dbread = dbcmd.ExecuteReader()

        Dim myitems As New List(Of SyndicationItem)
        While dbread.Read()
            Dim sItem As New SyndicationItem
            sItem.Title = makeRsstxt(dbread("cast"))
            sItem.Summary = makeRsstxt(dbread("description"))
            sItem.Links.Add(makeRsslink(dbread("link")))
            myitems.Add(sItem)
        End While

        myFeed.Items = myitems

        Dim feedWriter As XmlWriter = XmlWriter.Create(Response.OutputStream)
        Dim rssFormater = New Rss20FeedFormatter(myFeed)
        rssFormater.WriteTo(feedWriter)
        feedWriter.Close()
    End Sub

    Private Function okURL(ByVal urlStr As String)
        Return Request.Url.GetLeftPart(UriPartial.Authority) & ResolveUrl(urlStr)
    End Function

    Private Function makeRsstxt(ByVal str As String) As TextSyndicationContent
        Return TextSyndicationContent.CreatePlaintextContent(str)
    End Function

    Private Function makeRsslink(ByVal str1 As String) As SyndicationLink
        Return SyndicationLink.CreateAlternateLink(New Uri(str1))
    End Function
End Class
